package com.huazi.www.drug.drugy.util;

import java.sql.*;


public class BaseDAO {
    public static Connection conn;
    public static PreparedStatement ps;
    public static ResultSet rs;

    //    static {
//        init();
//    }
    // 使用流读取配置文件信息
//    public static void init(){
//        Properties properties=new Properties();
//        String str="Database.properties";
//        System.out.println(str);
//        InputStream is = BaseDAO.class.getClassLoader().getResourceAsStream(str);
//        try {
//            properties.load(is);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        driver=properties.getProperty("driver");
//        url=properties.getProperty("url");
//        user=properties.getProperty("user");
//        password=properties.getProperty("password");
//        try {
//            is.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    //加载驱动
    public Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=utf8", "root", "123456");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    //关闭的方法
    public void Close(ResultSet rs, PreparedStatement ps, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //增删改通用方法
    public int executeUpdate(String sql, Object... obj) {
        int count = 0;
        conn = getConnection();
        try {
            ps = conn.prepareStatement(sql);
            if (obj.length > 0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            count = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(null, ps, conn);
        }
        return count;
    }

    //查询通用方法
    public ResultSet executeQuery(String sql, Object... obj) {
        conn = getConnection();
        try {
            ps = conn.prepareStatement(sql);
            if (obj.length > 0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            rs = ps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rs;
    }
}
